/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deatax.display.gui;

import java.awt.Rectangle;

/**
 *
 * @author nrechten
 */
public class CPSConfig {

    public static final int LEFT = -1;
    public static final int WEST = -1;
    public static final int CENTER = 0;
    public static final int RIGHT = 1;
    public static final int EAST = 1;
    public static final int TOP = -1;
    public static final int NORTH = -1;
    public static final int BOTTOM = 1;
    public static final int SOUTH = 1;

    public int anchor_x = LEFT;
    public int anchor_y = TOP;

    public float x_percent = 0;
    public float y_percent = 0;

    public int x_pixel = 0;
    public int y_pixel = 0;

    public float w_percent = 0;
    public float h_percent = 0;

    public int w_pixel = 0;
    public int h_pixel = 0;

    public int w_max = Integer.MAX_VALUE;
    public int w_min = 0;

    public int h_max = Integer.MAX_VALUE;
    public int h_min = 0;

    public boolean useComponentMinMax = true;

    public CPSConfig() {
    }

    public CPSConfig(CPSConfig cps){
        this.anchor_x = cps.anchor_x;
        this.anchor_y = cps.anchor_y;
        this.x_percent = cps.x_percent;
        this.x_pixel = cps.x_pixel;
        this.y_percent = cps.y_percent;
        this.y_pixel = cps.y_pixel;
        this.w_max = cps.w_max;
        this.w_min = cps.w_min;
        this.w_percent = cps.w_percent;
        this.w_pixel = cps.w_pixel;
        this.h_max = cps.h_max;
        this.h_min = cps.h_min;
        this.h_percent = cps.h_percent;
        this.h_pixel = cps.h_pixel;
    }

    public CPSConfig(int anchor_x, int anchor_y, int x_pixel, int y_pixel, int w_pixel, int h_pixel){
        this(anchor_x, anchor_y,0,0,x_pixel,y_pixel,0,0,w_pixel,h_pixel);
    }

    public CPSConfig(int anchor_x, int anchor_y, float x_percent, float y_percent, float w_percent, float h_percent){
        this(anchor_x, anchor_y, x_percent, y_percent,0,0, w_percent, h_percent,0,0);
    }

    public CPSConfig(int anchor_x, int anchor_y, float x_percent, float y_percent, int x_pixel, int y_pixel, float w_percent, float h_percent, int w_pixel, int h_pixel){
        this.anchor_x = anchor_x;
        this.anchor_y = anchor_y;
        this.x_percent = x_percent;
        this.x_pixel = x_pixel;
        this.y_percent = y_percent;
        this.y_pixel = y_pixel;
        this.w_percent = w_percent;
        this.w_pixel = w_pixel;
        this.h_percent = h_percent;
        this.h_pixel = h_pixel;
    }

    public int calculateWidth(int parent_width){
        return Math.min(Math.max(Math.round(parent_width * w_percent)+w_pixel,w_min),w_max);
    }
    public int calculateHeight(int parent_height){
        return Math.min(Math.max(Math.round(parent_height * h_percent)+h_pixel,h_min),h_max);
    }
    public int calculateX(int parent_width, int width){
        int start = (anchor_x==LEFT?0:(anchor_x==CENTER?parent_width/2:parent_width));
        int offset = x_pixel + Math.round(x_percent * parent_width);
        return start+(anchor_x==LEFT?offset:(anchor_x==CENTER?offset-width/2:-offset-width));
    }
    
    public int calculateY(int parent_height, int width){
        int start = (anchor_y==LEFT?0:(anchor_y==CENTER?parent_height/2:parent_height));
        int offset = y_pixel + Math.round(y_percent * parent_height);
        return start+(anchor_y==LEFT?offset:(anchor_y==CENTER?offset-width/2:-offset-width));
    }
    

    public Rectangle calculateBounds(int parent_width, int parent_height){
        Rectangle bounds = new Rectangle(0, 0,calculateWidth(parent_width),calculateHeight(parent_width));

        bounds.x = calculateX(parent_width, bounds.width);
        bounds.y = calculateY(parent_height, bounds.height);
//        System.out.println(parent_width + "," + parent_height + ":" + bounds);

        return bounds;
    }


}
